package com.qf.homework.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri.endsWith("upload")){

            return true;
        }

        String remoteAddr = request.getRemoteAddr();
        if ("127.0.0.1".equals(remoteAddr)
                || "localhost".equals(remoteAddr)
                || "0:0:0:0:0:0:0:1".equals(remoteAddr)
        ){
            return true;
        }

        System.out.println("====== 有非法访问，来自：" + remoteAddr);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("<h2 style='color:red;'>无权访问,请交作业!!!</h2>");
        return false;
    }
}
